if (get_cookie("key")) {
    $.ajax({
        url: ip + "index.php?s=admincms/login/isadmin",
        type: "post",
        data: {
            "user": get_cookie("user"),
            "key": get_cookie("key")
        },
        success: function (_res) {
            res = JSON.parse(_res);
            if (res.resStatus === 0) {
                // 用户写入页面
                $("#user_name").text(res.data[0].NickName);
                $(".content_left>ul").empty();
                var url=location.href;
                var tiao=true;
                $.each(res.data[0].power_list,function (i,o) {
                    if(url.indexOf(o.url)!==-1){
                        tiao=false;
                    }
                    var _background_=  url.indexOf(o.url)!==-1?'style="background:#000;"':"";

                    $(".content_left>ul").append('<a href="'+o.url+'"> <li '+_background_+'><span class="glyphicon '+o.icon+'"></span><p>'+o.names+'</p></li></a>');

                });

                if(tiao){
                    location.href=res.data[0].power_list[0].url;
                }
            }
            if (res.resStatus === 1) {
                location.href = key_address;
            }
        },
        error: function () {
            location.href = key_address;
        }
    });
} else {
    location.href = key_address;
}
if (!localStorage.arr_danwei) {
    localStorage.arr_danwei = JSON.stringify([]);
}
if (!localStorage.arr_chicun) {
    localStorage.arr_chicun = JSON.stringify([]);
}
if (!localStorage.arr_color) {
    localStorage.arr_color = JSON.stringify([]);
}
$(function () {
    var houtai = {
        //防止频繁操作
        old_click_time: 1000,
        click_yes: function () {
            var now_tiem = new Date().getTime();
            if (now_tiem - houtai.old_click_time > 1000) {
                houtai.old_click_time = now_tiem;
                return true;
            } else {
                return false;
            }
        },
        linshi_search: {
            class1: "",
            class2: "",
            state: "",
            search: "",
            type:"",
            time_start:"",
            time_end:""
        },
        linshi_duiying: {
            search: "",
            haocai_text:"",
            id:"",
            tp:0
        },
        linshi_id: null,
        linshi_img_id: null,
        linshi_arrid: [],
        queren_type: null,//0删除 1上架 2下架 3全部上架 4全部下架 5删除图片 6上热卖 7下热卖
        list_ajax_page:1,
        //提示功能
        hint:function (str) {
            $(".hint").remove();
            $("body").append("<div class='hint'>"+str+"</div>");
            $(".hint").slideDown(300).delay(2000).slideUp();
        },
        jiancha: function () {
            // 判断是够为空
            for (var i = 0; i < $(".add_goods_body .input_is:visible").length; i++) {
                if ($(".add_goods_body .input_is:visible").eq(i).val() === "") {
                    $(".add_goods_body .input_is:visible").eq(i).focus();
                    houtai.hint("请填写 " + $(".add_goods_body .input_is:visible").eq(i).prev().text());
                    return false;
                }
            }
            //判断是否添加了供应商
            if($(".supplier_wrap>p").length===0){
                houtai.hint("请添加供应商");
                $("#shangpin_gongyingshang").focus();
                return false;
            }
            if (!isNaN($("#set_brand").val())) {
                houtai.hint("品牌不能只输入数字");
                $("#set_brand").focus();
                return false;
            }
            if (!isNaN($("#set_danwei").val())) {
                houtai.hint("单位不能输入数字");
                $("#set_danwei").focus();
                return false;
            }
            return true;
        },
        pinpai_fuzhi: function () {
            // 写入品牌
            var string_brand = "";
            var string_specs = "";
            var string_danwei = "";
            var string_chicun = "";
            var string_color = "";
            $.each(houtai.arr, function (i, o) {
                $.each(o.activity_list, function (i, o) {
                    if ($("#set-select2").val() == o.cid) {
                        if (o.brand.length > 0) {
                            $.each(o.brand, function (i, o) {
                                string_brand += '<li><a >' + o.names + '</a><p style="display:none;">' + o.bid + '</p><b style="display: none" title="删除" class="glyphicon glyphicon-minus"></b></li>';
                            });
                        }
                        $("#down_menu").empty().append(string_brand);
                        if (o.specs.length > 0) {
                            $.each(o.specs, function (i, o) {
                                string_specs += '<li><a >' + o.unit + '</a> <p style="display:none;">' + o.sid + '</p><b style="display:none;" title="删除" class="glyphicon glyphicon-minus"></b></li>';
                            });
                        }
                        $("#down_menu_guige").empty().append(string_specs);
                        if (o.color.length > 0) {
                            $.each(o.color, function (i, o) {
                                string_color += '<li><a >' + o.names + '</a> <p style="display:none;">' + o.id + '</p><b style="display:none;" title="删除" class="glyphicon glyphicon-minus"></b></li>';
                            });
                        }
                        $("#down_menu_color").empty().append(string_color);
                    }
                });
            });
            var arr = getarr(localStorage.arr_danwei);
            $.each(arr, function (i, o) {
                string_danwei += '<li><a >' + o + '</a> <b title="删除" class="glyphicon glyphicon-minus"></b></li>';
            });
            $("#down_menu_danwei").empty().append(string_danwei);
            var arr_chicun = getarr(localStorage.arr_chicun);
            $.each(arr_chicun, function (i, o) {
                string_chicun += '<li><a >' + o + '</a><b title="删除" class="glyphicon glyphicon-minus"></b></li>';
            });
            $("#down_menu_chicun").empty().append(string_chicun);
        },
        chushi_class: function () {
            $.ajax({
                url: ip + "index.php?s=admincms/interfaces/hcListClassShow",
                async: false,
                type: "post",
                data: {
                    token: get_cookie("key")
                },
                success: function (_res) {
                    var res = JSON.parse(_res);
                    if (res.resStatus === 0) {
                        houtai.arr = res.data;
                        for (var ii = 0, string11 ='<option value="">全部</option>'; ii < houtai.arr.length; ii++) {
                            string11 += '<option value="' + houtai.arr[ii].cate1 + '">' + houtai.arr[ii].name+ '</option>';
                        }
                        $("#select1").empty().append(string11);
                        $("#select2").empty().append('<option value="">全部</option>');
                    }
                },
                error: function () {
                    houtai.hint("初始类型失败了！！");
                }
            });
        },
        shop_list_ajax: function (page_number) {
            var n = page_number ? page_number : houtai.list_ajax_page=1;
            $.ajax({
                url: ip + "index.php?s=admincms/interfaces/hclistshow",
                type: "post",
                data: {
                    page: n,
                    cate1: houtai.linshi_search.class1,
                    cate2: houtai.linshi_search.class2,
                    tp: houtai.linshi_search.state,
                    search: houtai.linshi_search.search,
                    token: get_cookie("key")
                },
                success: function (_res) {
                    var res = JSON.parse(_res);
                    console.log(res);
                    houtai.table_string(res);
                    $("#page").sdpagination({
                        boxClass: 'sdpagination',
                        totalCount: res.totalpage,           //总条数
                        pageSize: 10,                //每页显示
                        pageIndex: n,                //当前页数
                        preText: '上页',                                  //上一页按钮显示的字符
                        nextText: '下页',                                 //下一页按钮显示的字符
                        firstText: '首页',                                //首页按钮显示的字符
                        lastText: '末页',                                 //末页按钮显示的字符
                        showNearby: 3,                                    //当前按钮前后按钮数
                        infoTemplet: '每页{pageSize} 共{totalCount} 当前{pageIndex}/{pageCount}',//自定义信息模板
                        pageList: [5, 10, 20],                            //设置PageSize下拉框的选择项，
                        onPageIndexChange: function (pageIndex) { //回调，点击按钮时或者输入页数时，激发onPageIndexChange事件，回调参数为 新页数、PageSize
                            houtai.list_ajax_page=pageIndex;
                            houtai.shop_list_ajax(pageIndex);
                        }
                    });

                }
            });
        },
        table_string: function (res) {
            // 写入table
            var string_head = "<thead><tr><th>编号</th><th>名称</th><th>品牌</th><th>价格</th><th>规格</th><th>单位</th><th>图片</th ><th>铭材名称</th ><th>铭材价格</th ><th>铭材图片</th ><th>操作</th></tr></thead>";
            var string_body = "";
            if (res.resStatus === 0) {
                $.each(res.data, function (i, o) {
                    var duiying_btn="";
                    var duiying_string="";
                    if(o.list_arr.is_bind===1){
                        duiying_btn='<b class="btn btn-danger cancle_duiying">取消对应</b>';
                        duiying_string= '<td style="vertical-align: middle;display: none" class="duiying_bianhao" >' + o.list_arr.product_list.id + '</td>' +
                                        '<td style="vertical-align: middle" >' +o.list_arr.product_list.name + '</td>' +
                                        '<td style="vertical-align: middle" >' +o.list_arr.product_list.price + '</td>' +
                                       '<td style="vertical-align: middle" ><img style="width: 70px" src="'+o.list_arr.product_list.img+'" alt=""></td>';
                    }else if(o.list_arr.is_bind===0){
                       duiying_btn='<b class="btn btn-primary  duiying">对应产品</b>';
                        duiying_string= '<td style="vertical-align: middle;display: none" class="duiying_bianhao">无</td>' +
                            '<td style="vertical-align: middle" >无</td>' +
                            '<td style="vertical-align: middle" >无</td>' +
                            '<td style="vertical-align: middle" >无</td>';
                    }
                    string_body += '<tr>' +
                        '<td style="vertical-align: middle" class="bian_hao">' + o.pid + '</td>' +
                        '<td style="vertical-align: middle" class="shangpin_name">' + o.title + '</td>' +
                        '<td style="vertical-align: middle" class="shangpin_pinpai">' + o.brand + '</td>' +
                        '<td style="vertical-align: middle" class="shangpin_price"><span>' + Math.float_(o.price,2) + '</span></td>' +
                        '<td style="vertical-align: middle" class="shangping_guige">' + o.specs + '</td>' +
                        '<td style="vertical-align: middle" class="shangping_danwei">' + o.unit + '</td>' +
                        '<td style="vertical-align: middle" class="shangpin_img"><img style="width:70px" src="' + o.img + '" alt=""></td>' +
                        duiying_string+
                        '<td style="vertical-align: middle;width: 200px;" class="text-center"> '+duiying_btn+'<p style="display:inline-block;margin:5px 2px;width: 80px;" class="btn btn-default btn-block record">价格记录</p></td>' +
                        '</tr>';
                });
            } else {
                string_body = "<tr class='text-center'><td colspan='14'>没有数据</td></tr>";
            }

            $("#shangpin_table").empty().append(string_head + string_body);
        },
        record_ajax: function (page_number) {
            var n = page_number ? page_number : 1;
            $.ajax({
                url: ip + "index.php?s=admincms/interfaces/priceRecord",
                type: "post",
                data: {
                     pid:houtai.linshi_id,
                    token: get_cookie("key"),
                    page:n,
                    start_time: houtai.linshi_search.time_start,
                    end_time: houtai.linshi_search.time_end
                },
                success: function (_res) {
                    var res = JSON.parse(_res);
                    console.log(res);
                    houtai.record_string(res);
                    $("#page1").sdpagination({
                        boxClass: 'sdpagination',
                        totalCount: res.totalpage,           //总条数
                        pageSize: 10,                //每页显示
                        pageIndex: n,                //当前页数
                        preText: '上页',                                  //上一页按钮显示的字符
                        nextText: '下页',                                 //下一页按钮显示的字符
                        firstText: '首页',                                //首页按钮显示的字符
                        lastText: '末页',                                 //末页按钮显示的字符
                        showNearby: 3,                                    //当前按钮前后按钮数
                        infoTemplet: '每页{pageSize} 共{totalCount} 当前{pageIndex}/{pageCount}',//自定义信息模板
                        pageList: [5, 10, 20],                            //设置PageSize下拉框的选择项，
                        onPageIndexChange: function (pageIndex) { //回调，点击按钮时或者输入页数时，激发onPageIndexChange事件，回调参数为 新页数、PageSize
                            houtai.record_ajax(pageIndex);
                        }
                    });

                }
            });
        },
        record_string: function (res) {
            // 写入table
            var string_head = "<thead><tr><th>价格</th><th>时间</th></tr></thead>";
            var string_body = "";
            if (res.resStatus === 0) {
                $.each(res.data, function (i, o) {
                    string_body += '<tr>' +
                        '<td style="vertical-align: middle" class="record_name">' + o.now_price + '</td>' +
                        '<td style="vertical-align: middle" class="record_time">' + o.time + '</td>' +
                        '</tr>';
                });
            } else {
                string_body = "<tr class='text-center'><td colspan='14'>没有数据</td></tr>";
            }

            $("#supervise_record table").empty().append(string_head + string_body);
        },
        duiying_list_ajax: function (page_number) {
            var n = page_number ? page_number : 1;
            $.ajax({
                url: ip + "index.php?s=admincms/interfaces/handBindShow",
                type: "post",
                data: {
                    page: n,
                    names:houtai.linshi_duiying.search,
                    token: get_cookie("key")
                },
                success: function (_res) {
                    var res = JSON.parse(_res);
                    console.log(res);
                    houtai.duiying_string(res);
                    $("#page2").sdpagination({
                        boxClass: 'sdpagination',
                        totalCount: res.totalpage,           //总条数
                        pageSize: 10,                //每页显示
                        pageIndex: n,                //当前页数
                        preText: '上页',                                  //上一页按钮显示的字符
                        nextText: '下页',                                 //下一页按钮显示的字符
                        firstText: '首页',                                //首页按钮显示的字符
                        lastText: '末页',                                 //末页按钮显示的字符
                        showNearby: 3,                                    //当前按钮前后按钮数
                        infoTemplet: '每页{pageSize} 共{totalCount} 当前{pageIndex}/{pageCount}',//自定义信息模板
                        pageList: [5, 10, 20],                            //设置PageSize下拉框的选择项，
                        onPageIndexChange: function (pageIndex) { //回调，点击按钮时或者输入页数时，激发onPageIndexChange事件，回调参数为 新页数、PageSize
                            houtai.list_ajax_page=pageIndex;
                            houtai.duiying_list_ajax(pageIndex);
                        }
                    });

                }
            });
        },
        duiying_string: function (res) {
            // 写入table
            var string_head = "<thead><tr><th>编号</th><th>名称</th><th>图片</th ><th>操作</th></tr></thead>";
            var string_body = "";
            if (res.resStatus === 0) {
                $.each(res.data, function (i, o) {
                    string_body += '<tr>' +
                        '<td style="vertical-align: middle" class="duiying_id">' + o.id + '</td>' +
                        '<td style="vertical-align: middle" class="duiying_name">' + o.names + '</td>' +
                        '<td style="vertical-align: middle"><img style="width: 50px" src="' + o.img + '" alt=""></td>' +
                        '<td style="vertical-align: middle;width: 200px;"> <b class="btn btn-default lianjie_duiying">对应</b></td>' +
                        '</tr>';
                });
            } else {
                string_body = "<tr class='text-center'><td colspan='14'>没有数据</td></tr>";
            }

            $("#duiying_table").empty().append(string_head + string_body);
        },
        duiying_confirm_ajax:function () {
            if(houtai.linshi_duiying.tp==0){
                $.ajax({
                    url:ip+"index.php?s=admincms/interfaces/handBind",
                    data:{
                        tp:0,
                        pid:houtai.linshi_id,
                        id :houtai.linshi_duiying.id,
                        token: get_cookie("key")
                    },
                    success:function (_res) {
                        var res = JSON.parse(_res);

                        if(res.resStatus === 0){
                            $("#modal_wrap").hide();
                            $("#supervise_duiying").hide();
                            houtai.shop_list_ajax(houtai.list_ajax_page);
                            houtai.hint("<p style='color:#0f0;'>对应成功！</p>");
                        }else{
                            houtai.hint(res.message);
                        }

                    },
                    error:function () {
                        houtai.hint("请求出错！！");
                    }
                });
            }else if(houtai.linshi_duiying.tp==1) {
                $.ajax({
                    url:ip+"index.php?s=admincms/interfaces/handBind",
                    data:{
                        tp:1,
                        pid:houtai.linshi_id,
                        id :houtai.linshi_duiying.id,
                        token: get_cookie("key")
                    },
                    success:function (_res) {
                        var res = JSON.parse(_res);

                        if(res.resStatus === 0){
                            $("#modal_wrap").hide();
                            houtai.shop_list_ajax(houtai.list_ajax_page);
                            houtai.hint("<p style='color:#0f0;'>取消对应成功！</p>");
                        }else{
                            houtai.hint(res.message);
                        }

                    },
                    error:function () {
                        houtai.hint("请求出错！！");
                    }
                });
            }
        }
    };
    // 初始类
    houtai.chushi_class();
    // 请求数据
    houtai.shop_list_ajax();
    //时间插件
    $('#input_time').daterangepicker({
        timePicker: true,
        timePickerIncrement: 30,
        locale: {
            format: 'YYYY-MM-DD HH:mm',
            applyLabel: '应用',
            cancelLabel: '取消',
            daysOfWeek: ["日", "一", "二", "三", "四", "五", "六"],
            monthNames: ["一月", "二月", "三月", "四月", "五月", "六月", "七月", "八月", "九月", "十月", "十一月", "十二月"]
        }
    });
    $("#input_time_btn").bind("click", function () {
        $("#input_time").click();
    });
    // 一级下拉事件
    $("#select1").bind("change", function () {
        if($(this).val()===""){
            $("#select2").empty().append('<option value="">全部</option>');
        }else{
            for (var b = 0; b < houtai.arr.length; b++) { //循环分类查找对应的子集
                if (houtai.arr[b].cate1 == $(this).val()) {
                    break;
                }
            }
            for (var a = 0, string2 = '<option value="">全部</option>'; a < houtai.arr[b].activity_list.length; a++) {
                string2 += '<option value="' + houtai.arr[b].activity_list[a].cate2 + '">' + houtai.arr[b].activity_list[a].name + '</option>';
            }
            $("#select2").empty().append(string2);
        }


        houtai.linshi_search.class1 = $("#select1").val();
        houtai.linshi_search.class2 = "";
        houtai.linshi_search.state = "";
        houtai.linshi_search.search = "";
        houtai.shop_list_ajax();
    });
    // 二级下拉事件
    $("#select2").bind("change", function () {
        houtai.linshi_search.class1 = $("#select1").val();
        houtai.linshi_search.class2 = $("#select2").val();
        houtai.linshi_search.state = "";
        houtai.linshi_search.search = "";
        houtai.shop_list_ajax();
    });
    // 状态下拉事件
    $("#select3").bind("change", function () {
        houtai.linshi_search.class1 = $("#select1").val();
        houtai.linshi_search.class2 = $("#select2").val();
        houtai.linshi_search.state = $(this).val();
        houtai.linshi_search.search = "";
        houtai.shop_list_ajax();
    });
    // 搜索本类
    $("#search_ben").bind("click", function () {
        if ($("#search_input").val() === "") {
            houtai.hint("请输入再搜索");
            $("#search_input").focus();
        } else {
            houtai.linshi_search.class1 = $("#select1").val();
            houtai.linshi_search.class2 = $("#select2").val();
            houtai.linshi_search.state = $("#select3").val();
            houtai.linshi_search.search = $.trim($("#search_input").val());
            houtai.shop_list_ajax();
        }

    });
    // 搜索全站
    $("#search_all").bind("click", function () {
        if ($("#search_input").val() === "") {
            houtai.hint("请输入再搜索");
            $("#search_input").focus();
        } else {
            houtai.linshi_search.class1 = "";
            houtai.linshi_search.class2 = "";
            houtai.linshi_search.state = "";
            houtai.linshi_search.search = $.trim($("#search_input").val());
            houtai.shop_list_ajax();
        }
    });

    // 收展事件
    $(".right-top>b").bind("click", function () {
        if ($(".content_left").is(":hidden")) {
            $(".content_left").removeClass("ishidden");
            $(".content_right").removeClass("iskuozhang");
        } else {
            $(".content_left").addClass("ishidden");
            $(".content_right").addClass("iskuozhang");
        }
    });

    // 莫态框取消modal
    $("#modal_close").bind("click", function () {
        $("#modal_wrap").fadeOut(200);

    });
    // 修改取消
    $("#cancle_set").bind("click", function () {
        $(".set_shangpin").fadeOut(200);
        $(".img_wrap").empty();
        $(".img_wrap_main").empty();
        $("#input_file").val("");
        $("#input_file_main").val("");
    });
    //取消记录框按钮
    $(".close").bind("click", function () {
        $(".set_shangpin").fadeOut();
    });
    //产品对应
    $("table").on("click", ".duiying", function () {
        $(window).scrollTop(0);
        houtai.linshi_id = $(this).parents("tr").find(".bian_hao").text();
        houtai.linshi_duiying.haocai_text = $(this).parents("tr").find(".shangpin_name").text();
        $("#duiying_table").empty();
        $("#input_duiying").val("");
        $("#page2").empty();
        $("#supervise_duiying").fadeIn();
    });
    //取消对应
    $("table").on("click", ".cancle_duiying", function () {
        houtai.linshi_id = $(this).parents("tr").find(".bian_hao").text();
        houtai.linshi_duiying.id = $(this).parents("tr").find(".duiying_biaohao").text();
        houtai.linshi_duiying.tp=1;
        $("#modal_wrap").show();
        $(".modal-body").empty().append("您确定要取消<span style='color:#68ca65;'>"+$(this).parents("td").siblings(".shangpin_name").text()+"</span>的对应吗？");

    });
    // 商品对应搜索
    $("#search_duiying").bind("click", function () {
        if ($.trim($("#input_duiying").val()) === "") {
            $("#input_duiying").focus();
            houtai.hint("请输入搜索内容");
        } else {
            houtai.linshi_duiying.search=$("#input_duiying").val();
            if (houtai.click_yes()) {
                houtai.duiying_list_ajax();
            }
        }

    });
    //连接对应
    $("#duiying_table").on("click", ".lianjie_duiying", function () {
     houtai.linshi_duiying.id=$(this).parents("td").siblings(".duiying_id").text();
     houtai.linshi_duiying.tp=0;
     $("#modal_wrap").show();
     $(".modal-body").empty().append("您确定要把<span style='color:#68ca65;'>"+$(this).parents("td").siblings(".duiying_name").text()+"</span>对应<span style='color:#f10;'>好材</span>的<span style='color:#f7b902;'>"+houtai.linshi_duiying.haocai_text+"</span>吗?");
    });
    //确认对应
    $("#confirm").bind("click",function () {
        if(houtai.click_yes()){
            houtai.duiying_confirm_ajax();
        }
    });
    //记录查询
    $("table").on("click", ".record", function () {
        $(window).scrollTop(0);
        houtai.linshi_id = $(this).parents("tr").find(".bian_hao").text();
        houtai.linshi_search.time_start="";
        houtai.linshi_search.time_end="";
        houtai.record_ajax();
        $("#supervise_record").fadeIn();
    });
    // 记录按时间搜索
    $("#search_time").bind("click", function () {
        if ($.trim($("#input_time").val()) === "") {
            $("#input_time").focus();
            houtai.hint("请输入搜索内容");
        } else {
            if (houtai.click_yes()) {
                console.log($("#input_time").val());
                // 匹配的正则表达式
                var regexp = /(\d+-\d+-[^-]+)-(.+)/;
                houtai.linshi_search.type="";
                houtai.linshi_search.time_start = new Date($("#input_time").val().match(regexp)[1]).getTime() / 1000;
                houtai.linshi_search.time_end = new Date($("#input_time").val().match(regexp)[2]).getTime() / 1000;
                houtai.record_ajax();
            }
        }

    });

    //退出按钮
    $("#exit").bind("click", function () {
        set_cookie("key", "", 0);
        location.href = key_address;
    });
    //头像按钮
    $(".right-top>div").bind("click", function (e) {
        $("#exit").slideToggle();
        e.stopPropagation();
    });
    $(window).bind("click", function () {
        $("#exit").slideUp();
    });
});

